//
//  KKIntroContentCell.m
//  DoMi
//
//  Created by 康凯 on 2016/11/10.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKIntroContentCell.h"
#import "KKStudyHttp.h"
#import "KKPersonalHomeVC.h"
#import "KKShareDialog.h"
#import "KKArticleDetailM.h"
#import "KKComHttp.h"
#import "KKMyHttp.h"

@interface KKIntroContentCell()
@property (weak, nonatomic) IBOutlet UIView *likeView;
@property (weak, nonatomic) IBOutlet UIView *collectView;
@property (weak, nonatomic) IBOutlet UIView *shareView;

@property (strong,nonatomic) KKVideoDetail *videoInfo;
@property (strong,nonatomic) KKArticleDetailM *articleInfo;
@property (assign,nonatomic) BOOL isCollected;
@property (weak, nonatomic) IBOutlet UIImageView *likeImg;
@property (weak, nonatomic) IBOutlet UIImageView *collectImg;
@property (copy,nonatomic)NSString *flag;
@property (copy,nonatomic)NSString *collectType;
@property (copy,nonatomic)NSString *number;
@property (copy,nonatomic)NSString *userId;

@end

@implementation KKIntroContentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(likeBtn)];
    [self.likeView addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectBtn)];
    [self.collectView addGestureRecognizer:tap2];
    UITapGestureRecognizer *tap3=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareBtn)];
    [self.shareView addGestureRecognizer:tap3];
    UITapGestureRecognizer *tap4=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushAvatar)];
    [self.avater addGestureRecognizer:tap4];
    
    
}

-(void)initData:(KKVideoDetail *)data{
    self.number=data.videoid;
    self.videoInfo=data;
    self.isCollected=[data.collectflag isEqualToString:@"0"]?NO:YES;
    self.title.text=data.videoname;
    self.content.text=data.videodesc;

    CGFloat labelHeight=[KKAppUtils sizeWithText:data.videodesc font:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(screenWidth-55, MAXFLOAT)].height;
    
//    NSNumber *count = @((labelHeight) / self.content.font.lineHeight);
    float line= labelHeight/self.content.font.lineHeight;
    NSLog(@"共%f",line);
    self.moreBtn.hidden=NO;
    if(line>2){
        self.moreBtn.hidden=NO;
    }else{
        self.moreBtn.hidden=YES;
    }
    
    self.time.text=[KKAppUtils timeStampToDate1:data.uploaddate];
    self.speaker.text=data.speaker;
    self.likeCount.text=data.videolike;
    self.collectCount.text=data.collect;
    self.shareCount.text=data.videoshare;
    
    if(self.isCollected){
        [self.collectImg setImage:[UIImage imageNamed:@"collectact"]];
    }else{
        [self.collectImg setImage:[UIImage imageNamed:@"collect"]];
    }
    
    if([data.likeflag isEqualToString:@"1"]){
        [self.likeImg setImage:[UIImage imageNamed:@"likeact"]];
    }else{
        [self.likeImg setImage:[UIImage imageNamed:@"like"]];
    }
    if([data.attentionflag isEqualToString:@"0"]){
        self.followBtn.selected=NO;
    }else{
        self.followBtn.selected=YES;
    }
    self.flag=@"0";
    self.userId=self.videoInfo.userid;
    [self.avater sd_setImageWithURL:[NSURL URLWithString:data.avatar] placeholderImage:[UIImage imageNamed:@"default_avater"]];
}

-(void)initArticleData:(KKArticleDetailM *)data{
    self.number=data.articleid;
    self.articleInfo=data;
    self.isCollected=[data.collectflag isEqualToString:@"0"]?NO:YES;
    self.title.text=nil;
//    self.content.text=data.videodesc;
    self.time.text=data.systime;
    self.speaker.text=data.nickname;
    self.likeCount.text=data.doclike;
    self.collectCount.text=data.collect;
    self.shareCount.text=data.docshare;
    
    if(self.isCollected){
        [self.collectImg setImage:[UIImage imageNamed:@"collectact"]];
    }else{
        [self.collectImg setImage:[UIImage imageNamed:@"collect"]];
    }
    
    if([data.likeflag isEqualToString:@"1"]){
        [self.likeImg setImage:[UIImage imageNamed:@"likeact"]];
    }else{
        [self.likeImg setImage:[UIImage imageNamed:@"like"]];
    }
    if([data.attentionflag isEqualToString:@"0"]){
        self.followBtn.selected=NO;
    }else{
        self.followBtn.selected=YES;
    }
    self.flag=@"1";
    self.userId=self.articleInfo.uid;
    [self.avater sd_setImageWithURL:[NSURL URLWithString:data.avatar] placeholderImage:[UIImage imageNamed:@"default_avater"]];
}

-(void)likeBtn{
    if([self.flag isEqualToString:@"0"]){
        if([self.videoInfo.likeflag isEqualToString:@"0"]){
            [self.likeImg setImage:[UIImage imageNamed:@"likeact"]];
        }
        [self addVideoLike:self.videoInfo.videoid];
    }else if([self.flag isEqualToString:@"1"]){
        if([self.articleInfo.likeflag isEqualToString:@"0"]){
            [self.likeImg setImage:[UIImage imageNamed:@"likeact"]];
        }
        [self addArticleLike:self.articleInfo.articleid];
    }
    
}

-(void)collectBtn{
    self.collectType=[self.flag isEqualToString:@"0"]?@"v":@"w";
    if(self.isCollected){
        [self.collectImg setImage:[UIImage imageNamed:@"collect"]];
        [self cancleCollect];
    }else{
        [self.collectImg setImage:[UIImage imageNamed:@"collectact"]];
        [self addCollect];
    }
    self.isCollected=!self.isCollected;
}
-(void)shareBtn{
    KKShareDialog *share=[[[NSBundle mainBundle] loadNibNamed:@"KKShareDialog" owner:self options:nil] lastObject];
    share.shareContent=@"xxxxxxx";
    share.shareCallbackBlock=^(int flag){
        if(flag==0){
            [KKAppUtils toast:@"分享失败"];
        }else{
            if([self.flag isEqualToString:@"0"]){
                [self shareVideoCallback];
            }else{
                [self shareArticleCallBack];
            }
            
        }
    };
    [share show];
}
#pragma mark 按钮--头像
-(void)pushAvatar{
    UIViewController *controller=[KKAppUtils getCurrentVisiableVC:self];
    KKPersonalHomeVC *personHome=[[KKPersonalHomeVC alloc] init];
    personHome.expertId=self.userId;    
    [controller.navigationController pushViewController:personHome animated:YES];
}
#pragma mark 按钮--关注
- (IBAction)followClick:(id)sender {
    if(self.followBtn.isSelected){
        [self cancelFollow];
    }else{
        [self addFollow];
    }
    self.followBtn.selected=!self.followBtn.isSelected;
}

#pragma mark 按钮--更多
- (IBAction)moreClick:(id)sender {
    if([self.moreBtn.titleLabel.text isEqualToString:@"更多"]){
        self.moreClickBlock(1);
        [self.moreBtn setTitle:@"收起" forState:UIControlStateNormal];
        self.content.numberOfLines=0;
    }else{
        self.moreClickBlock(0);
        [self.moreBtn setTitle:@"更多" forState:UIControlStateNormal];
        self.content.numberOfLines=2;
    }
}
#pragma mark 接口--点赞
-(void)addVideoLike:(NSString *)videoId{
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN,@"videoid":videoId};
    [KKStudyHttp addVideoLike:params success:^(id data) {
        self.likeCount.text=data;
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--文章点赞
-(void)addArticleLike:(NSString *)articleId{
    [KKComHttp commitArticleLike:articleId success:^(id data) {
        self.likeCount.text=data;
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--添加收藏
-(void)addCollect{
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN,@"collecttype":self.collectType,@"number":self.number};
    [KKStudyHttp addCollect:params success:^(id data) {
        self.collectCount.text=data;
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--取消收藏
-(void)cancleCollect{
    NSDictionary *params=@{UID_KEY:UID,TOKEN_KEY:TOKEN,@"collecttype":self.collectType,@"number":self.number};
    [KKStudyHttp cancleCollect:params success:^(id data) {
        self.collectCount.text=data;
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--分享回调
-(void)shareVideoCallback{
    [KKStudyHttp shareVideoCallBack:self.videoInfo.videoid success:^(id data) {
        self.shareCount.text=data;
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--分享文章点赞
-(void)shareArticleCallBack{
    [KKComHttp shareArticleCallBack:self.articleInfo.articleid success:^(id data) {
        self.shareCount.text=data;
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--增加关注
-(void)addFollow{
    [KKMyHttp addFollow:self.userId success:^(id data) {
        [KKAppUtils toast:@"关注成功"];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--取消关注
-(void)cancelFollow{
    [KKMyHttp cancelFollow:self.userId success:^(id data) {
        [KKAppUtils toast:@"取消关注成功"];
    } failure:^(NSError *error) {
        
    }];
}
@end
